package com.medvalley.tcm.weixin;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.medvalley.tcm.utils.HttpUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * Created by LinSong on 2018/3/20.
 */
public abstract class WXResultHandler {

    private static Logger LOGGER = LoggerFactory.getLogger(WXResultHandler.class);

    abstract JSONObject parseResult2Json(String responseString);

    public JSONObject retryDoWXRequest(String url, Map<String, String> para, int retryCount) throws WXException {
        for (int i = 0; i < retryCount; i++) {
            try {
                String responseString = HttpUtils.get(url, para);
                JSONObject result = parseResult2Json(responseString);
                return result;
            } catch (Exception e) {
                LOGGER.error("call wx server error,url:{},para:{},retryCount:{}", url, JSON.toJSONString(para), i, e);
            }
        }
        throw new WXException("call wx server error after retry");
    }

}
